﻿using CPFrameWork.Global;
using CPFrameWork.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using BusinessManage.Common;

namespace BusinessManage.SensorData
{
    public class GetHistoryDataController : CPWebApiBaseController
    {


        [HttpGet]
        public string GetSensorHistoryData(string GpsId = "SNS00005008", string SensorIds = "SNS00005007,SNS00005008", string starttime = "2018-11-01 00:00:00", string endtime = "2018-11-02 17:16:27")
        {
            //http://210.12.220.215:8081/datapublish/getPeriodSensorData.html?SN=SNS00005007&startTime=2018-01-01 00:00:00&endTime=2018-10-31 17:16:27
            string GPSdataUrl = string.Format(CPUtils.Configuration.GetSection("AppSettings")["SensorDataUrl"], GpsId, starttime, endtime);
            string GPSData = BusinessManage.Common.wphttppost.Get(GPSdataUrl);
            JObject gpsinfo = (JObject)JsonConvert.DeserializeObject(GPSData);//获取GPS数据
            JArray datalist = (JArray)gpsinfo["datalist"];
            if (datalist.Count > 0)
            {
                var gps = datalist[0];
                string[] sens = SensorIds.Split(',');
                foreach (string id in sens)
                {
                    string sendataurl = string.Format(CPUtils.Configuration.GetSection("AppSettings")["SensorDataUrl"], id, starttime, endtime);
                    var sdata = (JObject)JsonConvert.DeserializeObject(BusinessManage.Common.wphttppost.Get(sendataurl));
                    JArray slist = (JArray)sdata["datalist"];
                    if (slist.Count > 0)
                    {
                        JObject jo1 = new JObject();
                        var fielddim = gps["fielddim"].ToObject<List<string>>();//传感器值 的单位
                        foreach (string item in slist[0]["fielddim"])
                        {
                            fielddim.Add(item);
                        }
                        gps["fielddim"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(fielddim));//进行返回结果字段重新赋值，将传感器的字段加上
                        var fields = gps["sensorfield"].ToObject<List<string>>();//传感器值 的名称
                        foreach (string item in slist[0]["sensorfield"])
                        {
                            fields.Add(id + item);
                        }
                        gps["sensorfield"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(fields));//进行返回结果字段重新赋值，将传感器的字段加上
                        var valuelist = (JArray)gps["valuelist"];
                        for (int i = 0; i < valuelist.Count; i++)
                        {
                            valuelist[i] = AddData(valuelist[i], (JArray)slist[0]["valuelist"], id);
                        }
                        gps["valuelist"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(valuelist));

                    }
                    else
                    { continue; }
                }
                datalist[0] = gps;
                gpsinfo["datalist"] = datalist;
                return gpsinfo.ToString();
            }
            else
            {
                return "{\"Message\":\"nodata\"}";
            }
        }

        JToken AddData(JToken gpsdata, JArray SersorData, string id)
        {
            JObject gps = (JObject)gpsdata;
            foreach (JToken item in SersorData)
            {
                var sersordate = Convert.ToInt64(TimeStamp.GetTheTimeStamp(true, Convert.ToDateTime(item["time"])));
                var gpsdate = Convert.ToInt64(TimeStamp.GetTheTimeStamp(true, Convert.ToDateTime(gpsdata["time"])));

                if ((sersordate < gpsdate + 50) && (sersordate > gpsdate - 50))
                {
                    var value = gpsdata["fieldvalue"].ToObject<List<string>>();
                    value.Remove("0");//把GPS返回的数据中的0去掉
                    foreach (string va in item["fieldvalue"])
                    {
                        value.Add(va);

                    }
                    value.Remove("0");//把温湿度中返回的数据中的0去掉
                    gpsdata["fieldvalue"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(value));
                    gps = (JObject)gpsdata;
                    gps.Add(id, item["time"]);
                    return gps;
                }
            }
            return gpsdata;
        }

        [HttpGet]
        public string GetSensorCurrentData(string GpsId = "SNS00005008", string SensorIds = "SNS00005007,SNS00005008")
        {
            string GPSdataUrl = string.Format(CPUtils.Configuration.GetSection("AppSettings")["SnesorCurrent"], GpsId);
            string GPSData = BusinessManage.Common.wphttppost.Get(GPSdataUrl);
            JObject gpsinfo = (JObject)JsonConvert.DeserializeObject(GPSData);//获取GPS数据
            JArray datalist = (JArray)gpsinfo["datalist"];
            if (datalist.Count > 0)
            {
                var gps = datalist[0];
                string[] sens = SensorIds.Split(',');
                foreach (string id in sens)
                {
                    string sendataurl = string.Format(CPUtils.Configuration.GetSection("AppSettings")["SnesorCurrent"], id);
                    var sdata = (JObject)JsonConvert.DeserializeObject(BusinessManage.Common.wphttppost.Get(sendataurl));
                    JArray slist = (JArray)sdata["datalist"];
                    if (slist.Count > 0)
                    {
                        JObject jo1 = new JObject();
                        var fielddim = gps["fielddim"].ToObject<List<string>>();//传感器值 的单位
                        foreach (string item in slist[0]["fielddim"])
                        {
                            fielddim.Add(item);
                        }
                        gps["fielddim"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(fielddim));//进行返回结果字段重新赋值，将传感器的字段加上
                        var fields = gps["sensorfield"].ToObject<List<string>>();
                        foreach (string item in slist[0]["sensorfield"])
                        {
                            fields.Add(id + item);
                        }
                        gps["sensorfield"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(fields));//进行返回结果字段重新赋值，将传感器的字段加上
                        var valuelist = (JArray)gps["valuelist"];
                        var gpsdata = valuelist[0];
                        if (!string.IsNullOrEmpty(slist[0]["valuelist"].ToString()))
                        {
                            //valuelist[i] = AddData(valuelist[i], (JArray)slist[0]["valuelist"], id);
                            var value = gpsdata["fieldvalue"].ToObject<List<string>>();
                            value.Remove("0");//把GPS返回的数据中的0去掉
                            foreach (string va in slist[0]["valuelist"][0]["fieldvalue"])
                            {
                                value.Add(va);

                            }
                            value.Remove("0");//把温湿度中返回的数据中的0去掉
                            gpsdata["fieldvalue"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(value));
                            var newgps = (JObject)gpsdata;
                            newgps.Add(id, slist[0]["valuelist"][0]["time"]);
                            valuelist[0] = (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(newgps));
                        }
                        gps["valuelist"] = (JArray)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(valuelist));

                    }
                    else
                    { continue; }
                }
                datalist[0] = gps;
                gpsinfo["datalist"] = datalist;
                return gpsinfo.ToString();
            }
            else
            {
                return "{\"Message\":\"nodata\"}";
            }
            return null;
        }
    }
}
